package com.A1908.dao.impl;

import com.A1908.dao.RelationDao;
import com.A1908.entity.Relation;
import com.A1908.entity.RelationType;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RelationDaoImpl extends BaseDao implements RelationDao {
    Connection connection = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;

    @Override
    public List<RelationType> getAllRelationInfo() {
        connection = conn();
        String sql = "select * from relationtype;";
        Object[] objects = {};
        List<RelationType> list = new ArrayList<RelationType>();
        pstmt = pstmt(connection,sql,objects);
        try {
            rs = pstmt.executeQuery();
            while (rs.next()){
                RelationType relationType = new RelationType();
                relationType.setId(rs.getInt("id"));
                relationType.setRelationType(rs.getString("relationType"));
                relationType.setCreatDate(rs.getString("creatDate"));
                list.add(relationType);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            cloesAll(connection,pstmt,rs);
        }

        return list;
    }

    @Override
    public int addRelationInfo(Relation relation) {
        String sql="INSERT INTO relation VALUES (DEFAULT,?,?,?,?);";
        Object[]objs={relation.getHostUser(),relation.getRoleUser(),relation.getRelationNum(),relation.getRelationType()};
        int result=super.executeUpdate(sql,objs);
        return result;
    }

    @Override
    public int delRelation(int id) {
        return 0;
    }

    @Override
    public int updateRelation(Relation relation) {
        return 0;
    }
}
